// Ninf-G-XX, ver. 0.1
// Copyright 2011 National Institute of Advanced Industrial Science and Technology
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
 * ByteArrayOutputStream.hpp
 *
 *  Created on: Jan 29, 2009
 *      Author: nakada
 */

#ifndef BYTEARRAYOUTPUTSTREAM_HPP_
#define BYTEARRAYOUTPUTSTREAM_HPP_
#include <sys/types.h>
#include "OutputStream.hpp"


namespace ninf {

namespace lib {

class ByteArrayOutputStream : public OutputStream{
  bool closed;
  char * array;
  size_t offset;
  size_t current_size;
  size_t grow_size;
  bool freeOnDelete;
  /**
   * increase size of the buffer
   */
  bool grow(size_t size);

public:
  ByteArrayOutputStream(size_t initial_size = 10000, size_t grow_size = 10000, bool freeOnDelete = true);
  virtual ~ByteArrayOutputStream();
  /* write out a single byte */
  void write(int c);

  /* write out array of bytes */
  void write(const char * val, int offset, int len);

  void flush(){/* do nothing */}

  void close() {closed = true;}

  size_t length() {
    return offset;
  }

  /**
   * returns the buffer inside the object;
   * note that the buffer will be released
   * when this object is freed.
   */

  char * getBytes(){
    return array;
  }

  /**
   * for debug
   */
  void dump();

};

}

}

#endif /* BYTEARRAYOUTPUTSTREAM_HPP_ */
